package com.xxgc.wujiaoservice.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.xxgc.wujiaoservice.api.WechatLoginApi;
import com.xxgc.wujiaoservice.api.response.WechatLoginResponse;
import com.xxgc.wujiaoservice.controller.dto.UserLoginDTO;
import com.xxgc.wujiaoservice.controller.vo.UserLoginVO;
import com.xxgc.wujiaoservice.entity.UsersEntity;
import com.xxgc.wujiaoservice.entity.UsersEntityRepository;
import com.xxgc.wujiaoservice.result.Result;
import com.xxgc.wujiaoservice.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/* ResponseBody + Controller*/
@RestController
@RequestMapping("/login")
@Tag(name = "登录接口")
public class UserLoginController {
    @Resource
    UsersEntityRepository userEntityRepository;
    @Resource
    private UserService userService;

    private static final String AppID = "wx9dc4b526bc30eb1a";

    private static final String AppSecret = "bbcc7263e908cce470a970e55f782750";

    @Autowired
    private WechatLoginApi wechatLoginApi;


    @Operation(summary = "游戏用户登录")
    @PostMapping("/gameLogin")
    public Result gameLogin(@RequestBody Map map){
        System.out.println("map = " + map);
        QueryWrapper<UsersEntity> qw= new QueryWrapper<>();
        qw.eq("nickname",map.get("nickName"));
        qw.eq("password",map.get("password"));
        UsersEntity user = userEntityRepository.getOne(qw);
//        System.out.println("user = " + user);
        String id=null;
        String tokenValue;
        if (user!=null){
            id=user.getId();
            StpUtil.login(id);
            tokenValue = StpUtil.getTokenValue();
            return Result.success(tokenValue).msg("登录成功");
        }
        if (user == null){
            user = new UsersEntity();
            user.setNickname((String) map.get("nickName"));
            user.setPassword((String) map.get("password"));
            userEntityRepository.save(user);
            id=user.getId();
            StpUtil.login(id);
            tokenValue = StpUtil.getTokenValue();

            UserLoginDTO userInfo=new UserLoginDTO();
            userInfo.setId(user.getId());
            userInfo.setAvatar(user.getAvatar());
            userInfo.setNickname(user.getNickname());
            userInfo.setLevel(user.getLevel());
            userInfo.setToken(tokenValue);
            return Result.success(userInfo).msg("注册成功");
        }
        // 返回结果
        return Result.fail("2313").msg("出错了登陆");

    }










    @Operation(summary = "测试通讯用(不用登陆)")
    @GetMapping("/msgNoLogin")
    public Result msgNoLogin(){
        return Result.success("不需要登陆");
    }

    @Operation(summary = "测试通讯用(要登陆)")
    @GetMapping("/msgLogin")
    public Result msgLogin(){
        return Result.success("需要登陆");
    }

    @Operation(summary = "用户登录")
    @PostMapping("/userLogin")
    public UserLoginDTO userLogin(@RequestBody UserLoginVO userLoginVO){
        // 实现用户登录逻辑
        return new UserLoginDTO(); // 示例返回
    }

    @Operation(summary = "微信登录")
    @PostMapping("/wechatLogin/{code}")
    public UsersEntity wechatLogin(@PathVariable String code){
        System.out.println("微信登录");
        System.out.println("code = " + code);
        WechatLoginResponse authorization_code = wechatLoginApi.getUserOpenId(AppID, AppSecret, code, "authorization_code");
        System.out.println("authorization_code = " + authorization_code);


        // 开始登录1
   /*     UsersEntity user = userService.userLogin(authorization_code.getOpenid());
        System.out.println(user);
        if (user == null){
            // 用户不存在，创建用户
            user = new UsersEntity();
            user.setOpenId(authorization_code.getOpenid());
            userService.addUser(user);
        }*/

        //开始登陆2
        //mybatis-plus高级查询
        QueryWrapper<UsersEntity> qw= new QueryWrapper<>();
        qw.eq("open_id", authorization_code.getOpenid());
        UsersEntity user = userEntityRepository.getOne(qw);
        if (user == null){
            user = new UsersEntity();
            user.setOpenId(authorization_code.getOpenid());
            userEntityRepository.save(user);
        }
        // 返回结果
        return user;

    }
}
